package com.pactera.asmp.util;


import com.pactera.asmp.vo.AutherUserInfo;
import com.pactera.asmp.vo.MachineNo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 */
public class AuthUserConfigUtil {
    private static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    private static String defaultPath = System.getProperty("user.dir") + "\\config.xml";

    public static AutherUserInfo getConfig(String path) {
        if("".equals(path)||path == null){
            path = defaultPath;
        }
        AutherUserInfo autherUserInfo = new AutherUserInfo();
        Element rootElement = getDocument(path).getRootElement();
        Iterator<Element> iterator = rootElement.elementIterator();
        while (iterator.hasNext()) {
            Element ele = (Element) iterator.next();
            if (ele.getStringValue() != null && ele.getStringValue().length() > 0) {
                switch (ele.getName()) {
                    case "customerId":
                        autherUserInfo.setCustomerId(ele.getStringValue());
                        break;
                    case "customerName":
                        autherUserInfo.setCustomerName(ele.getStringValue());
                        break;
                    case "machineNos":
                        List<MachineNo> machineNos = new ArrayList<>();
                        Iterator<Element> iterator1 = ele.elementIterator();
                        while(iterator1.hasNext()){
                            Element ele1 = (Element) iterator1.next();
                            switch (ele1.getName()){
                                case "machineNo":
                                    MachineNo machineNo = new MachineNo();
                                    machineNo.setMachineNo(ele1.getStringValue());
                                    machineNos.add(machineNo);
                            }
                        }
                        autherUserInfo.setMachineNos(machineNos);
                        break;
                    case "startDate":
                        autherUserInfo.setStartDate(ele.getStringValue());
                        break;
                    case "endDate":
                        autherUserInfo.setEndDate(ele.getStringValue());
                        break;
                    case "sysType":
                        //1 window 2 linux 3 mac 4 solaris
                        autherUserInfo.setSysType(ele.getStringValue());
                        break;
                    default:
                        break;
                }
            }

        }
        return autherUserInfo;
    }



    public static Document getDocument(String path) {
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            document = saxReader.read(new File(path));
        } catch (DocumentException e) {
            logger.error("", e);
        }
        return document;
    }


}